package org.smherwig.wlanscanner;

import android.content.Context;
import android.graphics.Color;
import android.net.wifi.ScanResult;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ScanTableBuilder {
	private Context ctx;
	private TableLayout tableLayout;
	
	public ScanTableBuilder(Context ctx, TableLayout tableLayout) {
		this.ctx = ctx;
		this.tableLayout = tableLayout;
	}
	
	public void clearTable() {
		this.tableLayout.removeAllViews();
	}
	
	public void createHeader() {
		String[] fieldNames = {"SSID", "Level", "BSSID", "Capabilities", "Frequency"};
		TableRow header = new TableRow(ctx);
		header.setBackgroundColor(Color.GRAY);
		header.setLayoutParams(
				new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
		for (String fieldName : fieldNames) {
			TextView textView = new TextView(ctx);
			textView.setText(fieldName);
			header.addView(textView);
		}
		
		tableLayout.addView(header);
	}
	
	public void createScanRow(ScanResult scanResult) {
		TableRow row = new TableRow(ctx);
		row.setLayoutParams(
				new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
		TextView field = new TextView(ctx);
		field.setText(scanResult.SSID);
		row.addView(field);

		field = new TextView(ctx);
		field.setText("" + scanResult.level);
		row.addView(field);

		field = new TextView(ctx);
		field.setText(scanResult.BSSID);
		row.addView(field);

		field = new TextView(ctx);
		field.setText(scanResult.capabilities);
		row.addView(field);

		field = new TextView(ctx);
		field.setText("" + scanResult.frequency);
		row.addView(field);
		
		tableLayout.addView(row);
	}
}
